//
//  ViewController.m
//  UITest2
//
//  Created by 夏宇彬 on 15/9/19.
//  Copyright © 2015年 夏宇彬. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *bunnyView1;
@property (weak, nonatomic) IBOutlet UIImageView *bunnyView2;
@property (weak, nonatomic) IBOutlet UIImageView *bunnyViewMain;
@property (weak, nonatomic) IBOutlet UIImageView *bunnyView3;
@property (weak, nonatomic) IBOutlet UIImageView *bunnyView4;
@property (weak, nonatomic) IBOutlet UISlider *speedSlider;
@property (weak, nonatomic) IBOutlet UIStepper *speedStepper;
@property (weak, nonatomic) IBOutlet UILabel *hopsPerSecond;
@property (weak, nonatomic) IBOutlet UIButton *toggleButton;
- (IBAction)toggleAnimation:(id)sender;
- (IBAction)setSpeed:(id)sender;
- (IBAction)setIncrement:(id)sender;
- (IBAction)transferToWebView:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //初始化动画的图片
    NSMutableArray* hopAnimation = [NSMutableArray new];
    for (int i = 1; i <= 20; i++)
    {
        
        [hopAnimation addObject: [UIImage imageNamed:[NSMutableString stringWithFormat:@"frame-%d.png",i]]];
        
    }
    
    self.bunnyView1.animationImages = hopAnimation;
    self.bunnyView2.animationImages = hopAnimation;
    self.bunnyView3.animationImages = hopAnimation;
    self.bunnyView4.animationImages = hopAnimation;
    self.bunnyViewMain.animationImages = hopAnimation;
    
    self.bunnyView1.animationDuration = 1;
    self.bunnyView2.animationDuration = 1;
    self.bunnyView3.animationDuration = 1;
    self.bunnyView4.animationDuration = 1;
    self.bunnyViewMain.animationDuration = 1;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)toggleAnimation:(id)sender {
    //如果动画正在播放，停止
    if (self.bunnyView1.isAnimating) {
        [self.bunnyView1 stopAnimating];
        [self.bunnyView2 stopAnimating];
        [self.bunnyView3 stopAnimating];
        [self.bunnyView4 stopAnimating];
        [self.bunnyViewMain stopAnimating];
        [self.toggleButton setTitle:@"Hop!" forState:UIControlStateNormal];
    }
    //如果动画没有播放，则停止
    else{
        [self.bunnyView1 startAnimating];
        [self.bunnyView2 startAnimating];
        [self.bunnyView3 startAnimating];
        [self.bunnyView4 startAnimating];
        [self.bunnyViewMain startAnimating];
        [self.toggleButton setTitle:@"Sit Still!" forState:UIControlStateNormal];
    }
}

//通过滑块空间调整速度
- (IBAction)setSpeed:(id)sender {
    //显示速度的字符串
    NSString* hopRateString;
    
    //设置动画的持续时间
    self.bunnyViewMain.animationDuration = 2 - self.speedSlider.value;
    self.bunnyView1.animationDuration = self.bunnyViewMain.animationDuration + ((float)(rand()%10+1)/10);
    self.bunnyView2.animationDuration = self.bunnyViewMain.animationDuration + ((float)(rand()%10+1)/10);
    self.bunnyView3.animationDuration = self.bunnyViewMain.animationDuration + ((float)(rand()%10+1)/10);
    self.bunnyView4.animationDuration = self.bunnyViewMain.animationDuration + ((float)(rand()%10+1)/10);
    
    //动画开始
    [self.bunnyViewMain startAnimating];
    [self.bunnyView1 startAnimating];
    [self.bunnyView2 startAnimating];
    [self.bunnyView3 startAnimating];
    [self.bunnyView4 startAnimating];
    
    //修改按钮标题
    [self.toggleButton setTitle:@"Sit Still!" forState:UIControlStateNormal];
    
    //创建显示的字符串
    hopRateString = [NSString stringWithFormat:@"%1.2f hps", 1/(2 - self.speedSlider.value)];
    self.hopsPerSecond.text = hopRateString;
}

- (IBAction)setIncrement:(id)sender {
    self.speedSlider.value = self.speedStepper.value;
    [self setSpeed:nil];
}

- (IBAction)transferToWebView:(id)sender {
}
@end
